package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionProgramItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;


//소독계획서 쓰레드
public class DisinfectionProgramThread extends BasicThread {

	public interface DisinfectionProgramThreadCallback {
		public void OnDisinfectionProgramCompleted(boolean isNetworkConntected
				, DisinfectionProgramItem quarantineItem, String message);
		public void OnDisinfectionProgramError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionProgramThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String recNo;
	private String offCd;

	public DisinfectionProgramThread(Context context
			, DisinfectionProgramThreadCallback callback
			, String offCd
			, String recNo
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.recNo = recNo;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}
	
	@Override
	public void run() {
		Map<String, String> map = new HashMap<String,String>();
		String param = "";
		recNo = recNo.substring(3);
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ022, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionProgramCompleted(false, null, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionProgramError(null);
				return;
			}
			try {		
				int result = json.getInt(Const.RESULT);
				if(result == 0){	// result 값이 0일 경우
					String data = json.getString(Const.DATA);
					ErrorItem errorItem = new ErrorItem();
					errorItem.setrDesc(data);
					callback.OnDisinfectionProgramError(errorItem);
				}else{	// result 값이 0이 아닐 경우
					JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
					JSONObject disinfection = arraydisinfection.getJSONObject(0);
					DisinfectionProgramItem disinfectionItem = new DisinfectionProgramItem();
					disinfectionItem.setAllInsttNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.ALLINSTTNM)));
					disinfectionItem.setOffCd(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.OFFCD)));
					disinfectionItem.setRecNo(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.RECNO)));
					disinfectionItem.setRceptDate(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.RCEPTDATE)));
					disinfectionItem.setVenNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.VENNM)));
					disinfectionItem.setVenRep(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.VENREP)));
					disinfectionItem.setVenRnCode(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.VENRNCODE)));
					disinfectionItem.setVenRnAdres(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.VENRNADRES)));
					disinfectionItem.setVenRnAdresDetail(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.VENRNADRESDETAIL)));
					disinfectionItem.setTraDe(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.TRADE)));
					disinfectionItem.setNatNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.NATNM)));
					disinfectionItem.setItmNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.ITMNM)));
					disinfectionItem.setItmQty(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.ITMQTY)));
					disinfectionItem.setDikind1Nm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.DIKIND1NM)));
					disinfectionItem.setDikind2Nm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.DIKIND2NM)));
					disinfectionItem.setPreDe1(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.PREDE1)));
					disinfectionItem.setPreDe2(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.PREDE2)));
					disinfectionItem.setPreTime(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.PRETIME)));
					disinfectionItem.setDiplace(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.DIPLACE)));
					disinfectionItem.setMedNm(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.MEDNM)));
					disinfectionItem.setMedVol(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.MEDVOL)));
					disinfectionItem.setSanghos(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.SANGHOS)));
					disinfectionItem.setNames(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.NAMES)));
					disinfectionItem.setJobTime(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.JOBTIME)));
					disinfectionItem.setJobMin(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.JOBMIN)));
					disinfectionItem.setMedQty(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.MEDQTY)));
					disinfectionItem.setOpenDe(BrUtilManager.isNullCheck(disinfection.getString(DisinfectionProgramItem.OPENDE)));
					callback.OnDisinfectionProgramCompleted(true, disinfectionItem, mResult);
				}
			} catch (JSONException e) {
				
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();
		url = Const.MAIN_URL;
		Log.i("recNo","recNo =" + recNo);
		recNo = recNo.substring(3);
		post =  ParameterUtil.getDisinfectionPopupParams(offCd, recNo, 0);
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
	}
}
